// shop/goods/list  => shop_goods_list  
// 如果以index结尾,就去除index

let routes = [{
  path: '/',
  name: 'layout',
  redirect: {
    name: 'home'
  },
  // component: () => import('../../views/layout/index.vue'),
  component: 'layout/index',
  children: [{
    // path: '/index',
    // name: 'home',
    // component: () => import('../../views/home/index.vue'),
    meta: {
      title: '后台首页'
    },
    component: 'home/index'
  },
  {
    meta: {
      title: '相册管理'
    },
    component: 'image/index'
  },
  {
    meta: {
      title: '商品列表',
    },
    component: 'shop/goods/list'
  },
  {
    meta: {
      title: '创建商品'
    },
    component: 'shop/goods/create'
  },
  ]
},
{
  // path: '/login',
  // name: 'login',
  // component: () => import('../../views/login/index.vue'),
  meta: {
    title: '登录页'
  },
  component: 'login/index'
},
{
  path: '*',
  redirect: {
    name: 'home'
  },
}
]

// 获取路由信息方法
let get_routes = function () {
  // 自动生成路由
  create_route(routes)
  return routes
}

// 自动生成路由
function create_route(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (!arr[i].component) return

    // 2.去除结尾index的值
    let val = spite_index(arr[i].component)
    // 生成name
    arr[i].name = arr[i].name || val.replace(/\//g, '_')

    // // 3.生成path
    arr[i].path = arr[i].path || `/${val}`

    // 1.自动生成component
    let component_fun = import(`../../views/${arr[i].component}.vue`)
    arr[i].component = () => component_fun
    if (arr[i].children && arr[i].children.length > 0) {
      create_route(arr[i].children)
    }
  }
}

// 去除index结尾
function spite_index(str) {
  // 获取最后一个/的索引
  let index = str.lastIndexOf('/')
  // 获取最后一个/后面的值
  let val = str.substring(index + 1, str.length)
  // 判断是不是index结尾
  if (val === 'index') {
    return str.substring(index, -1)
  }
  return str
}


export default get_routes()